//
// Created by admin on 2021/10/24.
// @author 梦小星
//

#include "SqList.h"
#include <iostream>
#include <cstdlib>

void initList(SqList &list) {
    for (int i = 0; i < MAX_SIZE; ++i) {
        list.data[i] = 0;
    }
    list.length = 0;
}

int locateElem(SqList list, ElemType e) {
    for (int i = 0; i < list.length; ++i) {
        if (list.data[i] == e) return i;
    }
    return 0;
}

void listInsert(SqList &list, int position, ElemType e) {
    if (position < 1 || position > list.length + 1||list.length>=MAX_SIZE){
        std::cout << "error"<< std::endl;
        return;
    }
    for (int i = list.length-1; i >= position-1; --i) {
        list.data[i+1]=list.data[i];
    }
    list.data[position-1]=e;
    list.length++;
}

void listDelete(SqList &list, int position, ElemType &e) {
    if (position < 1 || position > list.length + 1||list.length==0) return;
    e=list.data[position-1];
    for (int i = position-1; i < list.length-1; ++i) {
        list.data[i]=list.data[i+1];
    }
    list.length--;
}

int length(SqList list) {
    return list.length;
}

ElemType getElem(SqList list, int position) {
    return list.data[position - 1];
}


void printList(SqList list) {
    for (int i = 0; i < list.length; ++i) {
        std::cout << list.data[i] << ",";
    }
    std::cout << std::endl;
}

void destroyList(SqList &list) {
    std::free(&list);
}

bool isListEmpty(SqList list) {
    if (list.length == 0) return true;
    return false;
}


